package com.micro.school.system.controller.student;

import java.util.List;

import com.micro.school.common.core.context.SecurityContextHolder;
import com.micro.school.common.core.domain.R;
import com.micro.school.common.core.utils.poi.ExcelUtil;
import com.micro.school.common.core.web.controller.BaseController;
import com.micro.school.common.core.web.domain.AjaxResult;
import com.micro.school.common.core.web.page.TableDataInfo;
import com.micro.school.common.log.annotation.Log;
import com.micro.school.common.log.enums.BusinessType;
import com.micro.school.system.api.domain.StudentInfos;
import com.micro.school.system.api.domain.vo.StudentInfoVos;
import com.micro.school.system.domain.StudentInfo;
import com.micro.school.system.domain.vo.StudentInfoOld;
import com.micro.school.system.utils.StudentInfoStatusUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.micro.school.system.domain.vo.StudentInfoVo;
import com.ruoyi.system.service.IStudentInfoService;

/**
 * 【请填写功能名称】Controller
 * 
 * @author rakic
 * @date 2025-08-05
 */
@RestController
@RequestMapping("/info")
public class StudentInfoController extends BaseController
{
    @Autowired
    private IStudentInfoService studentInfoService;

    /**
     * 查询所有学生列表
     */
    @GetMapping("/list")
    public TableDataInfo list(StudentInfoVo studentInfoVo)
    {
        startPage();
        List<StudentInfo> list = studentInfoService.selectStudentInfoList(studentInfoVo);
        return getDataTable(list);
    }
    /**
     * 查询所有新生信息
     *
     * */
    @GetMapping("/newlist")
    public TableDataInfo queryNewlist(StudentInfoVo studentInfoVo)
    {
        startPage();
        List<StudentInfoVo> list = studentInfoService.selectNewStudentInfoList(studentInfoVo);
        return getDataTable(list);
    }

    @PostMapping("/list/limitById")
    public R<List<StudentInfos>> listLimitById(@RequestBody StudentInfoVo studentInfoVo)
    {
        List<StudentInfos> list = studentInfoService.selectStudentInfoListLimit(studentInfoVo);

        return R.ok(list);
    }

    @PostMapping("/queryStudent")
    public AjaxResult queryStudent(@RequestBody StudentInfoVo studentInfoVo)
    {
        StudentInfos student = studentInfoService.queryStudent(studentInfoVo);
        return success(student);
    }


    @PostMapping("/queryVosByIdNumber")
    public AjaxResult queryVosByIdNumber(@RequestBody StudentInfoVos studentInfoVo)
    {
        List<StudentInfoVos> studentInfoVosList = studentInfoService.queryVosByIdNumber(studentInfoVo);
        return success(studentInfoVosList);
    }

    /**
     * 根据身份证号码查询学生信息（高频接口）
     */
    @Log(title = "根据身份证查询学生信息")
    @PostMapping("/queryByIdNumber")
    public AjaxResult queryByIdNumber(@RequestBody StudentInfoVo studentInfoVo) {
        // 参数校验
        if (studentInfoVo == null || studentInfoVo.getIdNumber() == null || studentInfoVo.getIdNumber().trim().isEmpty()) {
            return AjaxResult.error("身份证号码不能为空");
        }

        // 使用专门优化的方法查询学生信息
        List<StudentInfos> student = studentInfoService.queryStudentByidNumber(studentInfoVo.getIdNumber().trim());

        if (student != null) {
            return AjaxResult.success(student);
        } else {
            return AjaxResult.error("未找到该身份证号码对应的学生信息");
        }
    }

    /**
     * 导出学生列表
     */
    @Log(title = "导出学生列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StudentInfoVo studentInfoVo)
    {
        List<StudentInfo> list = studentInfoService.selectStudentInfoList(studentInfoVo);
        StudentInfoStatusUtil.processStudentInfoList(list);
        ExcelUtil<StudentInfo> util = new ExcelUtil<StudentInfo>(StudentInfo.class);
        util.exportExcel(response, list, "导出学生列表数据");
    }


    /**
     * 导出学生列表
     */
    @Log(title = "导出学生列表", businessType = BusinessType.EXPORT)
    @PostMapping("/newExport")
    public void newExport(HttpServletResponse response, StudentInfoVo studentInfoVo)
    {
        List<StudentInfoVo> list = studentInfoService.selectNewStudentInfoList(studentInfoVo);
        StudentInfoStatusUtil.processStudentInfoNewList(list);
        ExcelUtil<StudentInfoVo> util = new ExcelUtil<StudentInfoVo>(StudentInfoVo.class);
        util.exportExcel(response, list, "导出学生列表数据");
    }


    /**
     * 导出学生列表
     */
    @Log(title = "导出学生列表", businessType = BusinessType.EXPORT)
    @PostMapping("/oldExport")
    public void oldExport(HttpServletResponse response, StudentInfoVo studentInfoVo)
    {
        List<StudentInfoOld> list = studentInfoService.selectOldStudentInfoList(studentInfoVo);
        ExcelUtil<StudentInfoOld> util = new ExcelUtil<StudentInfoOld>(StudentInfoOld.class);
        util.exportExcel(response, list, "导出学生列表数据");
    }

    /**
     * 获取获取单个学生信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(studentInfoService.selectStudentInfoById(id));
    }

    /**
     * 获取获取单个学生信息详细信息
     */
    @PostMapping(value = "/queryStudentByIds")
    public R<List<StudentInfos>> queryStudentByIds(@RequestBody List<Long> ids)
    {
        List<StudentInfos> infos = studentInfoService.selectStudentByIds(ids);
        return R.ok(infos);
    }
    /**
     * 新增【请填写功能名称】
     */
    @Log(title = "新增学生信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StudentInfoVo studentInfoVo)
    {
        String info = studentInfoService.insertStudentInfo(studentInfoVo);
        if ("".equals(info)){
            return success();
        }else{
            return error(info);
        }
    }

    /**
     * 修改【请填写功能名称】
     */
    @Log(title = "修改学生信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StudentInfoVo studentInfoVo)
    {
        String updateStudentInfo = studentInfoService.updateStudentInfo(studentInfoVo);
        if ("".equals(updateStudentInfo)){
            return success();
        }else{
            return error(updateStudentInfo);
        }
    }

    /**
     * 删除【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long ids)
    {
        return toAjax(studentInfoService.deleteStudentInfoById(ids));
    }

    /**
     * 报名状态修改
     */
    @Log(title = "报名状态修改", businessType = BusinessType.UPDATE)
    @PostMapping("/sign")
    public AjaxResult sign(@RequestBody StudentInfoVo studentInfoVo)
    {
        int i = studentInfoService.signStudentInfoByIds(studentInfoVo);
        if (i > 0) {
            return toAjax(true);
        }else{
            return error("修改失败");
        }
    }

    /**
     * 封存状态修改
     */
    @Log(title = "封存状态修改", businessType = BusinessType.UPDATE)
    @GetMapping("/seal")
    public AjaxResult seal(StudentInfoVo studentInfoVo)
    {
        int i = studentInfoService.sealStudentInfoById(studentInfoVo);
        if (i > 0) {
            return toAjax(true);
        }else{
            return error("报名失败");
        }
    }

    /**
     * 批量转入在校生
     */
    @Log(title = "转入为在校生修改", businessType = BusinessType.UPDATE)
    @PostMapping("/in")
    public AjaxResult in(@RequestBody StudentInfoVo studentInfoVo)
    {
        int i = studentInfoService.inStudentInfoByIds(studentInfoVo);
        if (i > 0) {
            return toAjax(true);
        }else{
            return error("转入失败");
        }
    }

    /**
     * 封存状态修改
     */
    @Log(title = "报名状态批量修改", businessType = BusinessType.UPDATE)
    @PostMapping("/sealIds")
    public AjaxResult sealIds(@RequestBody StudentInfoVo studentInfoVo)
    {
        int i = studentInfoService.sealStudentInfoByIds(studentInfoVo);
        if (i > 0) {
            return toAjax(true);
        }else{
            return error("封存失败");
        }
    }

    /**
     * 修改学院id
     */
    @Log(title = "修改所有学生的学院id", businessType = BusinessType.UPDATE)
    @PostMapping("/updateAllStudentInfoCollegeName")
    public void updateAllStudentInfoCollegeName(@RequestParam("collegeName")String collegeName,@RequestParam("collegeId") Long collegeId,@RequestParam("oldCollegeName") String oldCollegeName)
    {
        int i = studentInfoService.updateAllStudentInfoCollegeName(collegeName,collegeId,oldCollegeName);

    }


    /**
     * 封存状态修改
     */
    @Log(title = "修改所有学生的学院id", businessType = BusinessType.UPDATE)
    @PostMapping("/updateAllCollegeMajor")
    public void updateAllCollegeMajor(@RequestParam("majorName")String majorName,@RequestParam("id") Long id,@RequestParam("oldMajorName") String oldMajorName)
    {
        int i = studentInfoService.updateAllCollegeMajor(majorName,id,oldMajorName);

    }


    /**
     * 封存状态修改
     */
    @Log(title = "修改所有学生的学院id", businessType = BusinessType.UPDATE)
    @PostMapping("/updateAllCollegeModel")
    public void updateAllCollegeModel(@RequestParam("modelName")String modelName,@RequestParam("id") Long id,@RequestParam("oldModelName") String oldModelName)
    {
        int i = studentInfoService.updateAllCollegeModel(modelName,id,oldModelName);

    }

    /**
     * 封存状态修改
     */
    @Log(title = "修改所有学生的学院id", businessType = BusinessType.UPDATE)
    @PostMapping("/updateAllCampusName")
    public void updateAllCampusName(@RequestParam("campusName")String campusName,@RequestParam("id") Long id,@RequestParam("oldCampusName") String oldCampusName)
    {
        int i = studentInfoService.updateAllCampusName(campusName,id,oldCampusName);

    }


    /**
     * 封存状态修改
     */
    @Log(title = "修改所有学生的学院id", businessType = BusinessType.UPDATE)
    @PostMapping("/updateAllSchoolGrade")
    public void updateAllSchoolGrade(@RequestParam("grade")String grade,@RequestParam("id") Long id)
    {
        int i = studentInfoService.updateAllSchoolGrade(grade,id);

    }






}
